#pragma once
#include<iostream>
#include <ctemplate/template.h>

#include"../comm/Log.hpp"
#include"../comm/Util.hpp"

namespace ns_view{
    using namespace ns_util;
    using namespace ns_log;
    using namespace ns_model;

    const std::string all_question_html_path = "./template_html/question_list.html";
    const std::string one_question_html_path = "./template_html/question_one.html";
    const std::string chat_page_html_path = "./template_html/chat_page.html";

    class View{
        private:
        public:
        void QuestionList(const std::vector<Question> &v,std::string *outhtml,const std::string &usr)
        {

            //创建字典
            ctemplate::TemplateDictionary root("all_question");
            root.SetValue("usr",usr);
            for(int i = 0;i < v.size();i++)
            {
                //创建子字典
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number",v[i].number);
                sub->SetValue("name",v[i].name);
                sub->SetValue("star",v[i].star);
            }
            
            //渲染配置
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(all_question_html_path,ctemplate::DO_NOT_STRIP);
            //开始渲染
            tpl->Expand(outhtml,&root);            
        }

        void OneQuestion(const Question &q,std::string *outhtml,const std::string &usr)
        {
            ctemplate::TemplateDictionary root("quetion_one");
            root.SetValue("usr",usr);
            root.SetValue("number",q.number);
            root.SetValue("name",q.name);
            root.SetValue("star",q.star);
            root.SetIntValue("cpu_limit",q.cpu_limit);
            root.SetIntValue("mem_limit",q.mem_limit);
            root.SetValue("desc",q.desc);
            root.SetValue("code",q.headers);

            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(one_question_html_path,ctemplate::DO_NOT_STRIP);
            tpl->Expand(outhtml,&root);
        }
        void ChatPage(std::string *outhtml,const std::string &usr)
        {
            ctemplate::TemplateDictionary root("chatpage");
            root.SetValue("usr",usr);
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(chat_page_html_path,ctemplate::DO_NOT_STRIP);
            tpl->Expand(outhtml,&root);
        }
    };

}
